/**
 * 员工基本信息管理
 * @author qiuxj
 */
var staffList = {
    init: function(dwr){
        // 增加按钮
        var addBtn = new CQCIS.Button({
            text: '添加 (<u>A</u>)',
            iconCls: 'btn_insert',
            handler: function(){
                    agentWin.show();
            }
        });
        
        // 修改按钮
        var editBtn = new CQCIS.Button({
            text: '修改 (<u>E</u>)',
            iconCls: 'btn_modify',
            handler: function(){
                if (Agrid.getSelectedRow()) {
                    agentWin.load(Agrid.getSelectedRow()['staffId']);
                } else {
                    CQCIS.msg("请选择要修改的员工！");
                }
            }
        });
        
        //定义分公司下拉列表
        var compList = new CQCIS.form.DWRComboBox({
            ds: {
                call: dwr.getCompanyKls
            },
            width: 120,
            emptyText: '请选择分公司',
            resizable: true,
            listeners: {
                'select': function(o){
                    Agrid.filter.companyId = o.getValue();
                    Agrid.reload();
                }
            }
        });
        
        // 添加员工信息表单字段
        var agentMeta = [{
            id: 'staffId',
            desc: '员工ID',
            field: 'staffId',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'upperStaffId',
            desc: '上级员工ID',
            type: 'int',
            xtype: 'hidden'
        }, {
            field: 'staffName',
            desc: '员工姓名',
            obj: {
                allowBlank: false
            }
        }, {
            field: 'sex',
            desc: '性别',
            type: 'int',
            xtype: 'acombo',
            obj: {
                ds: {
                    data: [['0', '保密'], ['1', '男'], ['2', '女']],
                    fields: ['key', 'label']
                },
                value: 0
            }
        }, {
            field: 'placeId',
            desc: '职位',
            type: 'int',
            xtype: 'rcombo',
            obj: {
                ds: {
                	params: {
        				state: 1,
        				staticTypeId: 10001
        			},
                    call: dwr.getStaticValueKls,
                    fields: ['key', 'label']
                },
                emptyText: '请选择职位',
                allowBlank: false
            }
        }, {
            field: 'age',
            desc: '年龄',
            type: 'int'
        }, {
            field: 'birthday',
            desc: '生日',
            xtype: 'datetimefield',
            type: 'date',
            obj: {
            	format: 'Y-m-d',
                allowBlank: false
            }
        }, {
            field: 'cardNo',
            desc: '证件号码'
        }, {
            field: 'companyId',
            desc: '分公司',
            type: 'int',
            xtype: 'rcombo',
            obj: {
                ds: {
                    call: dwr.getCompanyKls,
                    fields: ['key', 'label']
                },
                emptyText: '请选择分公司',
                allowBlank: false,
				listeners: {
                    'select': function(o){
                            agentWin.form.findField('departId').store.proxy.params = {companyId: o.getValue()};
                            agentWin.form.findField('departId').enable();
                            agentWin.form.findField('departId').store.reload();
                    }
                }
            },
            width: 80
        }, {
            field: 'departId',
            desc: '责任区域',
            type: 'int',
            xtype: 'rcombo',
            obj: {
				typeAhead: true,
                editable: true,
                forceSelection: true,
                minChars: 1,
				resizable: true,
				ds: {
					call: dwr.getAreaKls,
					fields: ['key', 'label']
				},
				disabled: true,
				emptyText: '请选择片区',
				allowBlank: false
			}
        }, {
            field: 'relaPhone',
            desc: '联系电话'
        }, {
            field: 'relaMobile',
            desc: '手机号码'
        }, {
            field: 'faxPhone',
            desc: '传真'
        }, {
            field: 'otherRelaPhone',
            desc: '其他号码'
        }, {
            field: 'relaAddr',
            desc: '联系地址'
        }, {
            field: 'email',
            desc: 'EMAIL'
        }, {
            field: 'state',
            desc: '状态',
            type: 'int',
            xtype: 'acombo',
            obj: {
                ds: {
                    data: [[0, '离职'], [1, '正常'], [2, '休假']],
                    fields: ['key', 'label']
                },
                value: 1
            }
        }];
		
        // 员工列表数据
        var agentListMeta = [{
            id: 'staffId',
            desc: '员工ID',
            field: 'staffId',
            type: 'int',
            hidden: true
        }, {
            field: 'upperStaffId',
            desc: '上级员工ID',
            type: 'int',
            hidden: true
        }, {
            field: 'staffName',
            desc: '员工姓名'
        }, {
            field: 'sex',
            desc: '性别',
            type: 'int',
            renderer: function(val){
                if (val == '0') {
                    return '保密';
                }
                if (val == '1') {
                    return '男';
                }
                if (val == '2') {
                    return '女';
                }
            }
        }, {
            field: 'placeName',
            desc: '职位'
        }, {
            field: 'age',
            desc: '年龄',
            type: 'int',
            renderer: function(val){
                if (!val) {
                    return '保密';
            	}
            	return val;
            }
        }, {
            field: 'birthday',
            desc: '生日',
            type: 'date'
        }, {
            field: 'cardNo',
            desc: '证件号码'
        }, {
            field: 'companyName',
            desc: '分公司'
        }, {
            field: 'departName',
            desc: '责任区域'
        }, {
            field: 'relaPhone',
            desc: '联系电话'
        }, {
            field: 'relaMobile',
            desc: '手机号码'
        }, {
            field: 'faxPhone',
            desc: '传真'
        }, {
            field: 'otherRelaPhone',
            desc: '其他号码'
        }, {
            field: 'relaAddr',
            desc: '联系地址'
        }, {
            field: 'email',
            desc: 'EMAIL'
        }, {
            field: 'state',
            desc: '状态',
            type: 'int',
            renderer: function(val){
	        	var str;
	            switch (val) {
	                case '0':
	                    str = '离职';
	                    break;
	                case '1':
	                    str = '正常';
	                    break;
	                case '2':
	                    str = '休假';
	                    break;
	                default:
	                    str = '正常';
	                    break;
	            }
	            return str;
            }
        }];
        // 定义表单
        var agentForm = new CQCIS.form.DWRForm({
            dwr: {
                load: dwr.getStaff,
                save: dwr.createStaff,
                update: dwr.updateStaff
            },
            metas: agentMeta,
            listeners: {
                'saveafter': function(){
                    CQCIS.msg('员工信息保存成功！');
                    Agrid.reload();
                }
            }
        });
        
        // 定义窗口
        var agentWin = new CQCIS.form.WinForm({
            title: '添加员工信息',
            width: 420,
            height: 480,
            items: agentForm
        });
        
        // 代理商列表
        var Agrid = new CQCIS.grid.PageGrid({
            el: 'staffList',
            title: '员工基本信息',
            firstColumn: 'radio',
            dwr: {
                call: dwr.pageStaff
            },
            filter: {},
            sortable: true,
            metas: agentListMeta,
            tbar: ['-', addBtn, '-', editBtn],
            bbar: true,
            initload: true,
            listeners: {
                'rowdblclick': editBtn.handler,
                'rowcontextmenu': function(o, rowIndex, e){
                    e.stopEvent();
                    //定义增加右键菜单
                    var addMenu = new Ext.menu.Item({
                        iconCls: 'menu_insert',
                        id: 'addMenu',
                        text: '增加',
                        handler: addBtn.handler
                    });
                    //定义修改右键菜单
                    var editMenu = new Ext.menu.Item({
                        iconCls: 'menu_modify',
                        text: '修改',
                        handler: editBtn.handler
                    });
                    //定义列表右键菜单
                    var gridMenu = new Ext.menu.Menu({
                        id: 'gridMenu',
                        items: [addMenu, editMenu]
                    });
                    gridMenu.showAt(e.getXY());
                },
                'render': function(o){
                    o.getSelectionModel().on('selectionchange', function(sm){
                        if (sm.getCount() > 0) {
                            vWorker.enable();
                        } else {
                            vWorker.disable();
                        }
                    });
                }
            }
        });
        return Agrid;
    }
};
